from django.core.management.base import BaseCommand, CommandError

# from mysqldb.models import PayAdminUserList, PayAdminGroupList
from mysqldb.models import AllinpayUserUserList, AllinpayAuthAdmin
from django_redis import get_redis_connection


def clear_cache():
    """清除缓存"""
    rds = get_redis_connection("sysconf")
    rds.delete("do_opencv")
    rds.delete("do_lipsync")
    rds.delete("do_video")
    rds.delete("do_audio")


def initialization():
    # # 管理组
    # PayAdminGroupList.objects.update_or_create(defaults={"name": "超级管理员"}, id=1)
    # PayAdminGroupList.objects.update_or_create(defaults={"name": "临时管理员"}, id=2)
    # # 管理员
    # PayAdminUserList.objects.update_or_create(
    #     defaults={
    #         "username": "admin",
    #         "phone": "13896370136",
    #         "email": "3361098@qq.com",
    #         "nicename": "超级管理员",
    #         "gender": 1,
    #         "status": 1,
    #         "group": 1,
    #         "is_super": 1,
    #     },
    #     id=1,
    # )

    # 管理员
    AllinpayAuthAdmin.objects.update_or_create(
        defaults={
            "password": "123456",
            "username": "admin",
            "phone": "13896370136",
            "email": "3361098@qq.com",
            "nicename": "超级管理员",
            "gender": 1,
            "status": 1,
            "is_super": 1,
            "is_authenticated": 1,
        },
        id=1,
    )


class Command(BaseCommand):
    help = "My custom startup command"

    def handle(self, *args, **kwargs):
        try:
            # put startup code here
            print("# [\033[1;35m 加载初始化数据 \033[0m] ...")
            # 清除缓存
            clear_cache()
            # 启动脚本
            initialization()
        except BaseException as err:
            print(err)
            raise CommandError("Initalization failed.")
